package io.github.wslxm.springbootplus2.manage.sys.dao.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.constant.RoleConst;
import io.github.wslxm.springbootplus2.manage.sys.dao.SysRoleDao;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysRoleMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysRole;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.next.SysUserRolesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class SysRoleDaoImpl implements SysRoleDao {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public String findRoleIdByCode(String code) {
        SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, code));
        return sysRole != null ? sysRole.getId() : null;
    }

    @Override
    public Map<String, String> findRoleIdsByCodes(List<String> codes) {
        List<SysRole> sysRoles = sysRoleMapper.selectList(new LambdaQueryWrapper<SysRole>().in(SysRole::getCode, codes));
        if (sysRoles.size() > 0) {
            return sysRoles.stream().collect(Collectors.toMap(SysRole::getCode, SysRole::getId,(existing, replacement) -> existing));
        }
        return new HashMap<>();
    }

    @Override
    public SysRole findSuperManage() {
        return sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, RoleConst.ROLE_SYS));
    }

    @Override
    public boolean isSuperManageByUserId(String userId) {
        List<SysUserRolesVO> roles = this.findRolesByUserId(userId);
        for (SysUserRolesVO role : roles) {
            if (role.getCode().equals(RoleConst.ROLE_SYS)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isSuperManageByRoleIds(List<String> roleIds) {
        List<SysRole> sysRoles = sysRoleMapper.selectBatchIds(roleIds);
        List<SysRole> list = sysRoles.stream().filter(p -> p.getCode().equals(RoleConst.ROLE_SYS)).toList();
        return list.size() > 0;
    }

    @Override
    public List<String> findRoleCodesByUserId(String userId) {
        List<SysUserRolesVO> roles = this.findRolesByUserId(userId);
        if (roles.size() > 0) {
            return roles.stream().map(SysUserRolesVO::getCode).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<SysUserRolesVO> findRolesByUserId(String userId) {
        Map<String, List<SysUserRolesVO>> rolesMap = this.findRolesByUserIds(CollUtil.newArrayList(userId));
        return rolesMap.size() > 0 ? rolesMap.get(userId) : new ArrayList<>();
    }


    @Override
    public Map<String, List<SysUserRolesVO>> findRolesByUserIds(List<String> userIds) {
        List<SysUserRolesVO> userRoles = sysRoleMapper.findRolesByUserIds(userIds);
        if (userRoles != null && userRoles.size() > 0) {
            return userRoles.stream().collect(Collectors.groupingBy(SysUserRolesVO::getUserId));
        } else {
            return new HashMap<>(0);
        }
    }
}
